package cn.iocoder.yudao.module.train.service.coursestudentregistform;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseStudentRegistFormDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.OrdersDTO;
import cn.iocoder.yudao.module.train.dal.mysql.coursecommodity.CourseCommodityMapper;
import cn.iocoder.yudao.module.train.dal.mysql.order.OrderMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.coursestudentregistform.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursestudentregistform.CourseStudentRegistFormDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.coursestudentregistform.CourseStudentRegistFormMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 班级学生 Service 实现类
 *
 * @author waynelam
 */
@Service
@Validated
public class CourseStudentRegistFormServiceImpl implements CourseStudentRegistFormService {

    @Resource
    private CourseStudentRegistFormMapper courseStudentRegistFormMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private CourseCommodityMapper courseCommodityMapper;

    @Override
    public Integer createCourseStudentRegistForm(CourseStudentRegistFormSaveReqVO createReqVO) {
        // 插入
        CourseStudentRegistFormDO courseStudentRegistForm = BeanUtils.toBean(createReqVO, CourseStudentRegistFormDO.class);
        courseStudentRegistFormMapper.insert(courseStudentRegistForm);

        // 返回
        return courseStudentRegistForm.getId();
    }

    @Override
    public void createCourseStudentRegistFormByIds(Long classesId, List<Integer> ids) {
        List<OrdersDTO> orderList = new ArrayList<>();
        for (Integer id : ids) {
            OrdersDTO ordersDTO = orderMapper.selectOrder(id);
            orderList.add(ordersDTO);
        }
        orderList.forEach(ordersDTO -> {
            orderMapper.changeClassesFlag(ordersDTO.getId(), 1);
            CourseStudentRegistFormDO courseStudentRegistFormDO = new CourseStudentRegistFormDO();
            courseStudentRegistFormDO.setStudentId(ordersDTO.getStudentId());
            courseStudentRegistFormDO.setClassesId(classesId);
            Integer duration = courseCommodityMapper.selectCourseCommodity(ordersDTO.getCommodityId()).getTeachingAllTime();
            courseStudentRegistFormDO.setDuration(duration);
            courseStudentRegistFormDO.setRestDuration(duration);
            courseStudentRegistFormMapper.insert(courseStudentRegistFormDO);
        });
    }

    @Override
    public void updateCourseStudentRegistForm(CourseStudentRegistFormSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseStudentRegistFormExists(updateReqVO.getId());
        // 更新
        CourseStudentRegistFormDO updateObj = BeanUtils.toBean(updateReqVO, CourseStudentRegistFormDO.class);
        courseStudentRegistFormMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseStudentRegistForm(Integer id) {
        // 校验存在
        validateCourseStudentRegistFormExists(id);
        // 删除
        courseStudentRegistFormMapper.deleteById(id);
    }

    @Override
    public void deleteCourseStudentRegistFormListByIds(List<Integer> ids) {
        // 删除
        courseStudentRegistFormMapper.deleteByIds(ids);
    }


    private void validateCourseStudentRegistFormExists(Integer id) {
        if (courseStudentRegistFormMapper.selectById(id) == null) {
            throw exception(COURSE_STUDENT_REGIST_FORM_NOT_EXISTS);
        }
    }

    @Override
    public CourseStudentRegistFormDO getCourseStudentRegistForm(Integer id) {
        return courseStudentRegistFormMapper.selectById(id);
    }

    @Override
    public PageResult<CourseStudentRegistFormDTO> getCourseStudentRegistFormPage(CourseStudentRegistFormPageReqVO pageReqVO) {
        return courseStudentRegistFormMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CourseStudentRegistFormDTO> getCourseStudentRegistFormListByClassesId(Long classesId) {
        return courseStudentRegistFormMapper.selectListByClassesId(classesId);
    }

    @Override
    public List<CourseStudentRegistFormDTO> getCourseStudentRegistFormListBystudentId(Long studentId) {
        return courseStudentRegistFormMapper.selectListByStudentId(studentId);
    }
}

